﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HighDiveAdmin.Resources;

namespace HighDiveAdmin.Engine
{
    /// <summary>
    /// Comparer Class used to sort a DiveList
    /// </summary>
    public class DiveListComparer : IComparer<Dive>
    {
        #region Properties

        private DiveListSortType _sortOnAttribute;
        private bool _descendingSort;

        #endregion

        #region Constructors

        /// <summary>
        /// Default sort on QueueOrderNumber
        /// </summary>
        /// <param name="descendingSort">Decides whether to sort descending or ascending</param>
        public DiveListComparer(bool descendingSort = true)
        {
            _sortOnAttribute = DiveListSortType.QueueOrderNumber;
            _descendingSort = descendingSort;
        }

        public DiveListComparer(DiveListSortType sortType, bool descendingSort = true)
        {
            _sortOnAttribute = sortType;
            _descendingSort = descendingSort;
        }

        #endregion

        #region Private Methods

        private int CompareTotalScore(Dive x, Dive y)
        {
            if (_descendingSort)
            {
                if (x.TotalScore > y.TotalScore)
                {
                    return 1;
                }
                if (x.TotalScore == y.TotalScore)
                {
                    return 0;
                }
                return -1;
            }
            else
            {
                if (x.TotalScore < y.TotalScore)
                {
                    return 1;
                }
                if (x.TotalScore == y.TotalScore)
                {
                    return 0;
                }
                return -1;
            }
        }

        private int CompareQueueOrderNumber(Dive x, Dive y)
        {
            if (_descendingSort)
            {
                if (x.QueueOrderNumber > y.QueueOrderNumber)
                {
                    return 1;
                }
                if (x.QueueOrderNumber == y.QueueOrderNumber)
                {
                    return 0;
                }
                return -1;
            }
            else
            {
                if (x.QueueOrderNumber < y.QueueOrderNumber)
                {
                    return 1;
                }
                if (x.QueueOrderNumber == y.QueueOrderNumber)
                {
                    return 0;
                }
                return -1;
            }
        }

        private int CompareDiverName(Dive x, Dive y)
        {
            int comparisonResult = String.Compare(x.Diver.FirstName, y.Diver.FirstName, StringComparison.Ordinal);
            if (comparisonResult == 0)
            {
                return CompareQueueOrderNumber(x, y);
            }
            if (_descendingSort)
            {
                return comparisonResult;
            }
            return -comparisonResult;
        }

        #endregion

        #region IComparer

        public int Compare(Dive x, Dive y)
        {
            switch (_sortOnAttribute)
            {
                case DiveListSortType.DiverName:
                    return CompareDiverName(x, y);
                case DiveListSortType.QueueOrderNumber:
                    return CompareQueueOrderNumber(x, y);
                case DiveListSortType.TotalScore:
                    return CompareTotalScore(x, y);
                default:
                    return 0;
            }
        }

        #endregion
    }
}
